#include "shmmanager.h"

ShmManager::ShmManager(int shmkey)
{
	m_shmkey = shmkey;
}

ShmManager::~ShmManager()
{
	pthread_mutex_destroy( &m_mutex );
}

int ShmManager::Creat(size_t size)
{
	int ret = -1;
	
	ret = pthread_mutex_init( &m_mutex, NULL );
	if ( ret == 0 )
	{
		m_shmid = shmget(m_shmkey,size,IPC_CREAT|0666);
		if (m_shmid != -1)
		{
			m_ps = shmat(m_shmid,NULL,0);
			if (m_ps != NULL)
			{
				ret = 0;
			}
		}
	}
	return ret;
}

int ShmManager::ShmDT()
{
	return shmdt(m_ps); 
}

int ShmManager::Remove()
{
	return shmctl(m_shmid,IPC_RMID,0);
}

void * ShmManager::GetShm()
{
	return m_ps;
}

int ShmManager::Lock()
{
	return pthread_mutex_lock( &m_mutex );
}

int ShmManager::UnLock()
{
	return pthread_mutex_unlock( &m_mutex ); 
}

int ShmManager::TryLock()
{
	return pthread_mutex_trylock( &m_mutex );
}
/*
int ShmManager::LockTimeOut(const struct timespec *restrict abs_timeout)
{
	return pthread_mutex_timedlock(&m_mutex,abs_timeout);
}
*/
 
